//package com.kmsoft.provider.config;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
//import org.springframework.context.ApplicationListener;
//import org.springframework.core.Ordered;
//import org.springframework.core.env.*;
//
//import java.util.Map;
//
//@Slf4j
//public class KmApplicationEnvironmentPreparedEvent implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
//
//    private static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE +9;
//
//    private static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";
//
//
//    @Override
//    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
//        if (event instanceof ApplicationEnvironmentPreparedEvent) {
//            ConfigurableEnvironment envi = event.getEnvironment();
//            MutablePropertySources prpoSource = envi.getPropertySources();
//            StringBuffer config = new StringBuffer("--");
//            config.append(CONFIG_LOCATION_PROPERTY);
//            config.append("=classpath:\\config\\");
//            resetCommonLineArgs(prpoSource, config);
//            resetSystemProperties(prpoSource, config);
//        }
//
//    }
//
//    private void resetCommonLineArgs(MutablePropertySources propSource, StringBuffer config) {
//        PropertySource<?> commandLineArgs = propSource.get("commandLineArgs");
//        if (null != commandLineArgs) {
//            String defaultLocation = commandLineArgs.getProperty(CONFIG_LOCATION_PROPERTY) != null
//                    ? (String) commandLineArgs.getProperty(CONFIG_LOCATION_PROPERTY)
//                    : null;
//            if (null != defaultLocation) {
//                config.append(",");
//                config.append(defaultLocation);
//                log.info("加载配置文件："+config.toString().substring(config.indexOf("=")+1));
//                PropertySource<?> newEnv = new SimpleCommandLinePropertySource(config.toString());
//                propSource.addFirst(newEnv);
//            }
//        }
//    }
//
//    @SuppressWarnings("unchecked")
//    private void resetSystemProperties(MutablePropertySources propSource, StringBuffer config) {
//        PropertySource<?> systemProperties = propSource.get("systemProperties");
//        if (null != systemProperties) {
//            Map<String, Object> pro = (Map<String, Object>) systemProperties.getSource();
//            String defaultConfig = pro.get(CONFIG_LOCATION_PROPERTY) != null
//                    ? (String) pro.get(CONFIG_LOCATION_PROPERTY)
//                    : null;
//            if (null != defaultConfig) {
//                config.append(",");
//                config.append(defaultConfig);
//                pro.put(CONFIG_LOCATION_PROPERTY, config.substring(config.indexOf("=")+1).toString());
//                log.info("加载配置文件："+defaultConfig);
//                MapPropertySource mapPropertySource = new MapPropertySource("systemProperties", pro);
//                propSource.addBefore("systemEnvironment", mapPropertySource);
//            }
//        }
//    }
//
//    @Override
//    public int getOrder() {
//        return DEFAULT_ORDER;
//    }
//}
